package com.lenovo.nova.widget.windowmanager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewManager;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;

import com.lenovo.nova.util.Applications;
import com.lenovo.nova.util.Applications.OnInputMethodShowOrHideListener;
import com.lenovo.nova.util.SuccessionEventSimulate;
import com.lenovo.nova.util.debug.MyOldLog;
import com.lenovo.nova.util.debug.ToastAlertUtil;
import com.lenovo.nova.util.debug.mylog;
import com.lenovo.nova.util.debug.slog;
import com.lenovo.nova.utils.R;

import java.util.ArrayList;
import java.util.List;

public class WindowManagerArrow implements OnTouchListener, OnInputMethodShowOrHideListener, ArrowManagerInterface {
    private static final String TAG = WindowManagerArrow.class.getSimpleName();

    private static final int DEFAULT_IMAGE_HEIGHT = 42;
    private static final int DEFAULT_IMAGE_WIDTH = 70;
	private boolean isViewAlreadyShow;

	public static final int ARROW_ID_LEFT = 0;
	public static final int ARROW_ID_RIGHT = 1;
	public static final int ARROW_ID_TOP = 2;
	public static final int ARROW_ID_BOTTOM = 3;
	
	
	private Context mContext;
	private WindowManager.LayoutParams mTopParams = null;
	private WindowManager.LayoutParams mBottomParams = null;
	private WindowManager.LayoutParams mLeftParams = null;
	private WindowManager.LayoutParams mRightParams = null;

	protected ImageView mTopView = null;
	protected ImageView mBottomView = null;
	protected ImageView mLeftView = null;
	protected ImageView mRightView = null;

	private View mOnTouchView;

    private WindowManager mManager;

    public static List<WindowManagerArrow> mAllInstance = new ArrayList<WindowManagerArrow>();

    public WindowManagerArrow(Context context,boolean isHorizontal) {
        init(context,isHorizontal);
        mAllInstance.add(this);
    }

    public int getLeftImageWidth() {
        return mLeftView.getWidth();
    }
    public int getLeftImageHeight() {
        return mLeftView.getHeight();
    }

    public int getTopImageViewHeight() {
        if (mTopView != null && mTopView.getDrawable() != null) {
            return mTopView.getDrawable().getIntrinsicHeight();
        }
        slog.d("SettingsArrowManager getTopImageViewHeight use default imageview");
        return DEFAULT_IMAGE_HEIGHT;
    }
    public int getTopImageViewWidth() {
        if (mTopView != null && mTopView.getDrawable() != null) {
            return mTopView.getDrawable().getIntrinsicWidth();
        }
        slog.d("SettingsArrowManager getTopImageViewWidth use default imageview");
        return DEFAULT_IMAGE_WIDTH;
    }


    public void setImageViewParamsGravity(int top, int bottom) {
        if (mBottomParams != null && mTopParams != null) {
            mTopParams.gravity = top;
            mBottomParams.gravity = bottom;
        } else {
            ToastAlertUtil.alertError("Error params is null " + mBottomParams + "  " + mTopView, mContext);
        }
    }

    public void setLeftVisible(boolean visibility) {
		setViewVisible(mLeftView, visibility);
	}
	public void setRightVisible(boolean visibility) {
		setViewVisible(mRightView, visibility);
	}
	public void setTopVisible(boolean visibility) {
		setViewVisible(mTopView, visibility);
	}
	public void setBottomVisible(boolean visibility) {
		setViewVisible(mBottomView, visibility);
	}

	public void setLeftDrawable(Drawable drawable){
		mLeftView.setImageDrawable(drawable);
	}
	public void setRightDrawable(Drawable drawable){
		mRightView.setImageDrawable(drawable);
	}
    public void setTopDrawable(Drawable drawable){
        mTopView.setImageDrawable(drawable);
    }
    public void setBottomDrawable(Drawable drawable){
        mBottomView.setImageDrawable(drawable);
    }
	public void setParamType(int type) {
		mBottomParams.type = type;
		mTopParams.type = type;
	}

	public void close() {
        mylog.i(TAG,"isViewAlreadyShow " + isViewAlreadyShow);
		if (isViewAlreadyShow) {
			try {
                remove(mTopView);
                remove(mBottomView);
                remove(mLeftView);
                remove(mRightView);
			} catch (Exception e) {
				mylog.e(TAG, e.toString());
			}
		} else {
			mylog.e(TAG, "error isViewAlreadyShow " + isViewAlreadyShow + " mTopView " + mTopView + " mBottomView " + mBottomView);
		}

        isViewAlreadyShow = false;
	}

	public void release() {
		mSimulate.release();
	}

	public void setOnClickListener(OnClickListener l){
        if(mLeftView != null){
            mLeftView.setOnClickListener(l);
        }

        if(mRightView != null){
            mRightView.setOnClickListener(l);
        }

        if(mTopView != null){
            mTopView.setOnClickListener(l);
        }

        if(mBottomView != null){
            mBottomView.setOnClickListener(l);
        }
	}

    @Override
	public boolean onTouch(View v, MotionEvent event) {
		slog.d("SettingsArrowManager onTouch");
		mOnTouchView = v;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			mSimulate.start();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			mSimulate.stop();
			mOnTouchView = null;
		}
		return false;
	}

	@Override
	public void onInputMethodChange(boolean show) {
		if (mBottomView != null) {
			if (show) {
				mBottomView.setVisibility(View.INVISIBLE);
			} else {
				mBottomView.setVisibility(View.VISIBLE);
			}
		}
	}

    private void update() {
        try {
            update(mTopView, mTopParams);
            update(mBottomView, mBottomParams);
            update(mLeftView, mLeftParams);
            update(mRightView, mRightParams);
        } catch (Exception e) {
            MyOldLog.e(e);
        }
    }

    private void addView(View view,WindowManager.LayoutParams params){
        if(view != null && params != null){
            getWindowManager().addView(view, params);
        }
    }

    private WindowManagerArrow show() {
        boolean isAlive = Applications.get().isActivityAlive();
        mylog.i(TAG,"isAlive " + isAlive + " isViewAlreadyShow " + isViewAlreadyShow);
        if ( isAlive && !isViewAlreadyShow) {
            addView(mTopView, mTopParams);
            addView(mBottomView, mBottomParams);
            addView(mLeftView, mLeftParams);
            addView(mRightView, mRightParams);
        }

        isViewAlreadyShow = true;
        return this;
    }

	private void update(View view, WindowManager.LayoutParams params) {
		if (view != null && view.getVisibility() == View.VISIBLE) {
			getWindowManager().updateViewLayout(view, params);
		}
	}

    private void remove(View view){
        if(view != null){
            getWindowManager().removeView(view);
        }
    }

    private void setViewVisible(View view, boolean visibility) {
		if (view != null) {
			view.setVisibility(visibility ? View.VISIBLE : View.GONE);
		}
	}

	@SuppressLint("ServiceCast")
	private ViewManager getWindowManager() {
		if (mManager == null) {
			mManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		}
		return mManager;
	}

    public void showArrowYPosition(float topViewY, float bottomViewY) {
        mylog.d(TAG,"showArrowYPosition " +mTopView + "  " +
                (mTopView != null ?  mTopView.getParent() :"null") );
        if (mTopView == null || mTopView.getParent() == null) {
            show();
        }
        mTopParams.y = (int) topViewY;
        mBottomParams.y = (int) bottomViewY;
        update();
    }
    public void showArrowXPosition(float x) {
        if (mTopView == null || mTopView.getParent() == null) {
            show();
        }
        mTopParams.x = (int) x;
        mBottomParams.x = (int) x;
        update();
    }

    public void setLeftAndRightArrowX(int leftX,int rightX){
        if(!isViewAlreadyShow){
            show();
        }
        mLeftParams.x = leftX;
        mRightParams.x = rightX;
        update();
    }
    public void setLeftAndRightArrowY(int y){
        if(!isViewAlreadyShow){
            show();
        }
        mLeftParams.y = y;
        mRightParams.y = y;
        update();
    }

    private void init(Context context,boolean isHorizontal){
        isViewAlreadyShow = false;
        mContext = context;
        if(isHorizontal){
            mLeftParams = new WindowManager.LayoutParams();
            mRightParams = new WindowManager.LayoutParams();
            createLeftImageView();
            createRightImageView();

        }else {
            mTopParams = new WindowManager.LayoutParams();
            mBottomParams = new WindowManager.LayoutParams();
            createTopImageView();
            createBottomView();
        }

        mSimulate.prepare();
    }

    private void createRightImageView() {
        Drawable drawable = mContext.getResources().getDrawable(R.drawable.xml_arrow_page_right);

        mRightView = new ImageView(mContext);
        mRightView.setTag(ARROW_ID_RIGHT);
//        if (mRightView == null) {
//        }

        doCommonSetView(mRightView,drawable);
		doCommonSetLayoutParam(mRightParams);
	}
	private void createLeftImageView() {
        Drawable drawable = mContext.getResources().getDrawable(R.drawable.xml_arrow_page_left);

        mLeftView = new ImageView(mContext);
        mLeftView.setTag(ARROW_ID_LEFT);
//        if (mLeftView == null) {
//        }
        doCommonSetView(mLeftView,drawable);
		doCommonSetLayoutParam(mLeftParams);
	}
	private void createBottomView() {
        Drawable drawable = mContext.getResources().getDrawable(R.drawable.xml_arrow_page_down);
        mBottomView = new ImageView(mContext);
        mBottomView.setTag(ARROW_ID_BOTTOM);
        doCommonSetView(mBottomView,drawable);
		doCommonSetLayoutParam(mBottomParams);

	}
	private void createTopImageView() {
        Drawable drawable = mContext.getResources().getDrawable(R.drawable.xml_arrow_page_up);
        mTopView = new ImageView(mContext);
        mTopView.setTag(ARROW_ID_TOP);
//        if (mTopView == null) {
//        }
        doCommonSetView(mTopView,drawable);
		doCommonSetLayoutParam(mTopParams);
		mTopParams.y = 100;
	}

	private void doCommonSetView(View view, Drawable drawable) {
		view.setOnTouchListener(this);
		((ImageView) view).setImageDrawable(drawable);
	}
	private void doCommonSetLayoutParam(LayoutParams params) {
		params.format = PixelFormat.RGBA_8888;
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		// params.x = X;

		params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_KEEP_SCREEN_ON | LayoutParams.FLAG_LAYOUT_IN_SCREEN | LayoutParams.FLAG_LAYOUT_NO_LIMITS;
		// params.type = LayoutParams.TYPE_INPUT_METHOD
		params.type = LayoutParams.TYPE_APPLICATION_PANEL;
		// params.type = LayoutParams.TYPE_PHONE;
		params.gravity = Gravity.TOP | Gravity.LEFT;
		;
	}

    private SuccessionEventSimulate mSimulate = new SuccessionEventSimulate() {
		@Override
		public void looper() {
			if (mOnTouchView != null) {
			} else {
				mSimulate.stop();
			}
		}
	};
}
